package com.wei.miaosha.controller;

import com.wei.miaosha.domain.MiaoshaUser;
import com.wei.miaosha.domain.OrderInfo;
import com.wei.miaosha.result.CodeMsg;
import com.wei.miaosha.result.Result;
import com.wei.miaosha.service.GoodsService;
import com.wei.miaosha.service.OrderService;
import com.wei.miaosha.util.LogUtils;
import com.wei.miaosha.vo.GoodsVo;
import com.wei.miaosha.vo.OrderDetailVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Weizichen on 2018/9/1.
 * desc:订单控制层
 */
@Controller
@RequestMapping("/order")
public class OrdersController {
    private static Logger logger = LogManager.getLogger("OrdersController");
    @Autowired
    OrderService orderService;
    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "/detail")
    @ResponseBody
    public Result<OrderDetailVo> detatil(MiaoshaUser user, @RequestParam(value = "orderId", defaultValue = "0") long orderId) {
        if (user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo order = orderService.getOrderById(orderId);
        if (order == null) {
            return Result.error(CodeMsg.ORDER_NOT_EXIST);
        }
        //查找商品是否存在
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(order.getGoodsId());
        if (goods == null) {
            return Result.error(CodeMsg.GOODS_NOT_EXIST);
        }
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setGoods(goods);
        orderDetailVo.setOrder(order);
        LogUtils.infoLog(logger, " " + "-" + " " + "-" + " " + "success" + " " + "订单信息");
        return Result.success(orderDetailVo);
    }
}
